package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;



    @Override
    public Page<OrdersDto> dtoPage(Integer page , Integer pageSize) {
        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);
        return  ordersMapper.dtoPage(ordersDtoPage,BaseContext.getCurrentId());
    }

    @Transactional
    @Override
    public void submitOrders(Orders orders) {
        AddressBook addr = addressBookService.lambdaQuery()
                .eq(AddressBook::getId,orders.getAddressBookId())
                .one();
        User u = userService.lambdaQuery()
                .eq(User::getId, BaseContext.getCurrentId())
                .one();
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .list();
        Long id = IdWorker.getId();
        BigDecimal total  = new BigDecimal(0);
        List<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCart cart : list) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(id);
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setImage(cart.getImage());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setName(cart.getName());
            orderDetail.setSetmealId(cart.getSetmealId());

            orderDetails.add(orderDetail);

            total = total.add(cart.getAmount());
        }
        orderDetailService.saveBatch(orderDetails);



        orders.setId(id);
        orders.setNumber(String.valueOf(id));
        orders.setUserId(BaseContext.getCurrentId());
        orders.setStatus(2);
        orders.setAddress(addr.getDetail());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setConsignee(u.getName());
        orders.setAmount(total);
        orders.setPhone(u.getPhone());
        orders.setUserName(u.getName());
        this.save(orders);

        shoppingCartService.lambdaUpdate()
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .remove();
    }
}
